package cn.doyens.reggie.controller;

import cn.doyens.reggie.common.BaseContext;
import cn.doyens.reggie.common.CustomException;
import cn.doyens.reggie.common.R;
import cn.doyens.reggie.entity.AddressBook;
import cn.doyens.reggie.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;

    /**
     *  查询当前用户的地址簿列表信息
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        //1.获取当前登陆者的 id
        Long userId = BaseContext.getCurrentId();
        //2.创建条件构造器对象
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        //3.通过 addressBookService 调用方法获取符合条件的 条目信息集合
        List<AddressBook> addressBookList = this.addressBookService.list(queryWrapper);
        return R.success(addressBookList);
    }


    /**
     *   新增地址功能方法
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
       //1.为当前的addressBook对象的userid字段赋值
        addressBook.setUserId(BaseContext.getCurrentId());
        //2.通过 addressBookService调用 save方法 完成添加操作
        this.addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     *  设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public  R<String> setDefault(@RequestBody  AddressBook addressBook){
        //1.修改当前用户下所有的地址 的isDefault字段值为 0
        Long userId = BaseContext.getCurrentId();
        //创建修改条件构造器对象
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(AddressBook::getUserId,userId);
        updateWrapper.set(AddressBook::getIsDefault,0);
       //通过 AddressBookService调用方法执行修改操作
        this.addressBookService.update(updateWrapper);

        //2.修改指定地址的isDefault字段为 1
        addressBook.setUserId(userId);
        addressBook.setIsDefault(1);
        this.addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

    /**
     *  查询并返回当前用户的默认地址
     * @return
     */
    @GetMapping("/getDefault")
    public R<AddressBook>  get(){
        //1.获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //2.创建条件构造器对象
        LambdaQueryWrapper<AddressBook> queryWrapper  =new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDefault,1);

        //3.通过addressBookService调用方法获取符合条件的对象
        AddressBook addressBook = this.addressBookService.getOne(queryWrapper);
        if(addressBook==null){
            throw  new CustomException("您还未设置默认地址 ");
        }
        return R.success(addressBook);

    }

}
